package com.hjqjl.whdemo.dialog.dialogfragment;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.hjqjl.whdemo.R;

//设置属性和点击事件可参考自定义布局CustomDialogFragment
public class DialogBottomFragment extends DialogFragment {

    public static DialogBottomFragment newInstance() {
        DialogBottomFragment fragment = new DialogBottomFragment();
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //去掉dialog的标题，需要在setContentView()之前
        Window window = null;
        if (getDialog() != null) {
            this.getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
            window = this.getDialog().getWindow();
            if (window != null) {
                //去掉dialog默认的padding
                window.getDecorView().setPadding(0, 0, 0, 0);
                WindowManager.LayoutParams lp = window.getAttributes();
                lp.width = WindowManager.LayoutParams.MATCH_PARENT;
                lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
                //设置dialog的位置在底部
                lp.gravity = Gravity.BOTTOM;
                //设置dialog的动画
                lp.windowAnimations = R.style.BottomDialogAnimation;
                window.setAttributes(lp);
                window.setBackgroundDrawable(new ColorDrawable());
            }
        }
        final View view = inflater.inflate(R.layout.dialog_bottom, container);
        return view;
    }
}
